package com.foreveross.weixin.util;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class ObjectToMap {
	
	/**
	 * 将对象转化成map对象
	 * 
	 * @param obj
	 * @param showNull
	 * @return
	 * @throws IllegalAccessException
	 */
	public static Map<String, String> objectToMapViaFields(Object obj,Boolean showNull)
			throws IllegalAccessException {
		Map<String, String> resMap = new HashMap<String, String>();
		Field[] declaredFields =obj.getClass().getDeclaredFields();
		for (Field field : declaredFields) {
			field.setAccessible(true);
			
			if(field.get(obj) != null){
				resMap.put(field.getName(), field.get(obj).toString());
			}else if(field.get(obj) == null && showNull){
				resMap.put(field.getName(), "");
			}
			
		}
		return resMap;
	}

 
	private static void printOut(Map<String, Object> resMap) {
		for (String key : resMap.keySet()) {
			System.out.println("Key:" + key + ";Value:" + resMap.get(key));
		}
	}
	
}
	

